﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Top.Web;
using Top.PointStore.Business;
using Top.PointStore.Business.Models;
using Top.PointStore.Web.Filters;
using Top.Web.Api;
using Top.Web.Api.Filters;

namespace Top.PointStore.Controllers
{
    //[Login]
    [Provider]
    public class ChartController : ApiBase
    {
        //
        // GET: /Chart/

        public object Get_PointReport_Data(string reportType = "tag", string daysNum = "1")
        {
            return ChartManager.Get_PointReport_Data(reportType, daysNum);
        }
        [Login]
        public object Get_PointReport_Data2(string reportType, int daysNum, string startTime = "", string endTime = "",string cityName="")
        {
            //int graphicType,int dateNum,string startTime,string endTime
            //Chart/Get_PointReport_Data\
            try
            {
                switch (reportType)
                {
                    case "pv":
                        switch (daysNum)
                        {
                            case 1:
                                return ChartManager.PV_GetOneDay(DateTime.Now, cityName, pUser.PointStoreAuth);
                            case 7:
                                return ChartManager.PV_GetNDay(GetWeekStartTime(DateTime.Now).ToString("yyyy-MM-dd"), GetWeekStartTime(DateTime.Now).AddDays(7).ToString("yyyy-MM-dd"), cityName, pUser.PointStoreAuth);
                            case 30:
                                return ChartManager.PV_GetNDay(GetMonthStartTime(DateTime.Now).ToString("yyyy-MM-dd"), GetMonthStartTime(DateTime.Now).AddMonths(1).ToString("yyyy-MM-dd"), cityName, pUser.PointStoreAuth);
                            case 365:
                                return ChartManager.PV_GetOneYear(GetYearStartTime(DateTime.Now).ToString("yyyy-MM-dd"), GetYearStartTime(DateTime.Now).AddYears(1).ToString("yyyy-MM-dd"), cityName, pUser.PointStoreAuth);
                            case 0:
                                return ChartManager.PV_UserDefined(startTime, Convert.ToDateTime(endTime).AddDays(1).ToString("yyyy-MM-dd"), cityName, pUser.PointStoreAuth);
                            default:
                                return ChartManager.PV_GetOneDay(DateTime.Now, cityName, pUser.PointStoreAuth);
                        }
                    case "uv":
                        switch (daysNum)
                        {
                            case 1:
                                return ChartManager.UV_GetOneDay(DateTime.Now, cityName, pUser.PointStoreAuth);
                            case 7:
                                return ChartManager.UV_GetNDay(GetWeekStartTime(DateTime.Now).ToString("yyyy-MM-dd"), GetWeekStartTime(DateTime.Now).AddDays(7).ToString("yyyy-MM-dd"), cityName, pUser.PointStoreAuth);
                            case 30:
                                return ChartManager.UV_GetNDay(GetMonthStartTime(DateTime.Now).ToString("yyyy-MM-dd"), GetMonthStartTime(DateTime.Now).AddMonths(1).ToString("yyyy-MM-dd"), cityName, pUser.PointStoreAuth);
                            case 365:
                                return ChartManager.UV_GetOneYear(GetYearStartTime(DateTime.Now).ToString("yyyy-MM-dd"), GetYearStartTime(DateTime.Now).AddYears(1).ToString("yyyy-MM-dd"), cityName, pUser.PointStoreAuth);
                            case 0:
                                return ChartManager.UV_UserDefined(startTime, Convert.ToDateTime(endTime).AddDays(1).ToString("yyyy-MM-dd"), cityName, pUser.PointStoreAuth);
                            default:
                                return ChartManager.UV_GetOneDay(DateTime.Now, cityName, pUser.PointStoreAuth);
                        }
                    case "tag":
                        switch (daysNum)
                        {
                            case 1:
                                return ChartManager.TAG_GetData(DateTime.Now.Date.ToString("yyyy-MM-dd"), DateTime.Now.AddDays(1).Date.ToString("yyyy-MM-dd"), cityName,pUser.PointStoreAuth);
                            case 7:
                                return ChartManager.TAG_GetData(GetWeekStartTime(DateTime.Now).ToString("yyyy-MM-dd"), GetWeekStartTime(DateTime.Now).AddDays(7).ToString("yyyy-MM-dd"), cityName,pUser.PointStoreAuth);
                            case 30:
                                return ChartManager.TAG_GetData(GetMonthStartTime(DateTime.Now).ToString("yyyy-MM-dd"), GetMonthStartTime(DateTime.Now).AddMonths(1).ToString("yyyy-MM-dd"), cityName,pUser.PointStoreAuth);
                            case 365:
                                return ChartManager.TAG_GetData(GetYearStartTime(DateTime.Now).ToString("yyyy-MM-dd"), GetYearStartTime(DateTime.Now).AddYears(1).ToString("yyyy-MM-dd"), cityName,pUser.PointStoreAuth);
                            case 0:
                                return ChartManager.TAG_GetData(startTime, Convert.ToDateTime(endTime).AddDays(1).ToString("yyyy-MM-dd"), cityName,pUser.PointStoreAuth);
                            default:
                                return ChartManager.TAG_GetData(DateTime.Now.Date.ToString("yyyy-MM-dd"), DateTime.Now.AddDays(1).Date.ToString("yyyy-MM-dd"), cityName,pUser.PointStoreAuth);
                        }
                    default:
                        return null;
                }        
            }
            catch (Exception ex)
            {                
                throw;
            }
                         
        }



        //-------------------------------------------------------------工具------------------------------------------------------------
        /// <summary>
        /// 获取本周的开始日期
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        private DateTime GetWeekStartTime(DateTime time)
        {
            int weekNo = (int)time.DayOfWeek;
            if (weekNo==0)
            {
                weekNo += 7;
            }
            time = time.AddDays(-weekNo + 1);
            return time;
        }
        /// <summary>
        /// 获取本月的开始日期
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        private DateTime GetMonthStartTime(DateTime time)
        {
            int year = time.Year;
            int month = time.Month;
            DateTime monthStartTime = Convert.ToDateTime(year + "-" + month + "-1");
            return monthStartTime;
        }
        /// <summary>
        /// 获取本年的开始日期
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        private DateTime GetYearStartTime(DateTime time)
        {
            int year = time.Year;
            DateTime yearStartTime =Convert.ToDateTime(year + "-1-1");
            return yearStartTime;
        }
        
    }
}
